<?php
/**
 * This file is part of the Agora distribution. 
 * Detailed copyright and licensing information can be found
 * in the gpl-3.0.txt file which should be included in the distribution.
 * 
 * @version		$Id: 3.0.14 Olympus 2010-09-28 rammstein4o $
 * @copyright  2007 - 2010 jVitals
 * @license   GPLv3 Open Source
 * @link       http://jvitals.com
 * @since      File available since initial release
 */	
defined ('IN_AGORA') or die; 
class TaskController extends AgoraAdminController
{
	function loadModel()
	{
		$this->model = & Model::getInstance('StylesModel');
	}

	function _default()
	{
		$this->setPagination($this->model->loadAllCount());

		$this->model->setOrder('ORDER BY ref_name ASC');
		$styles = $this->model->_loadAll('',$this->pagination->per_page,$this->pagination->page);
		$this->view->assign('styles',$styles);
		$this->view->template='configuration/styles';
	}

	function add()
	{
		$style = array('id'=>0,'ref_name'=>'','params'=>'');

		$this->view->assign('style',$style);
		$this->view->assign('type','clean');
		$this->view->template='configuration/edit_style';
	}

	function edit()
	{
		$cid = Agora::getPostVar('cid');
		if (is_array($cid)) {
			$id = array_shift($cid);
		} else {
			$id = Agora::getVar('id');
		}
		$style = $this->model->load($id);

		$this->view->assign('style',$style);
		$this->view->template='configuration/edit_style';
	}

	function save()
	{
		$style = Agora::getPostVar('form',array());
		$id = Agora::getPostVar('id',0);

		if (trim($style['ref_name']) == '') {
			Agora::showError(Agora::lang('Style ref name cannot be empty'));
			$this->redirect('task=configuration');
		}

		if (!$id) {
			$exists = $this->model->load($style['ref_name'],'ref_name');
			
			if ( ($exists) ) {
				Agora::showError(Agora::lang('Style with this ref name already exists'));
				$this->redirect('task=configuration');
			}
			$this->model->add($style);
		} else {
			$this->model->edit($id,$style);
		}

		Agora::showMessage(Agora::lang('Style saved'));
		$this->redirect('task=configuration');
	}

	function cancel()
	{
		$this->redirect('task=configuration');
	}

	function remove()
	{
		$ids = Agora::getPostVar('cid',array());
		if (!is_array($ids)) {
			Agora::showError('Bad request');
			$this->redirect();
		}

		foreach($ids as $id) {
			$this->model->delete($id);
		}

		$this->redirect();
	}
}

